package cruds.tires;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.IDataProvider;

import services.StockableService;
import home.Home;
import items.Stockable;
import cruds.ParentList;
import cruds.accessories.AccListTH;
import cruds.accessories.CrudModifyAccessory;
import dataproviders.StockableDataProvider;
import dynamicmenus.TiresMenu;

@SuppressWarnings("serial")
public class TiresList extends ParentList<Stockable>{

	public TiresList(Home home, List<Stockable> items) {
		super(home, items);
	}

	@Override
	protected void initializePanelTH(Form form) {
		form.add(new AccListTH());
		
	}

	@Override
	protected void updateDynamicMenu(AjaxRequestTarget target) {
		TiresMenu menu = new TiresMenu(this.getHome());
		menu.changePanel(target);
	}

	@Override
	protected void doUpdateAndRedirect(AjaxRequestTarget target,Item<Stockable> item) {
		CrudModifyTire panel = new CrudModifyTire(this.getHome(),item.getModelObject());
		panel.changePanel(target);
	}

	@Override
	protected void doDeleteAndRedirect(AjaxRequestTarget target,Item<Stockable> item) {
		Stockable accessory = item.getModelObject();
		this.getService().delete(accessory);
		StockableService service = ((StockableService)this.getService());
		TiresList listar = new TiresList(this.getHome(),service.searchTires(accessory.getBrand()));
		listar.changePanel(target);
		
	}

	@Override
	protected void createFormFields(Item<Stockable> item) {
		item.add(new Label("brand",item.getModelObject().getBrand()));
		item.add(new Label("model",item.getModelObject().getModel()));
		item.add(new Label("measure",item.getModelObject().getMeasure()));
		item.add(new Label("listprice",String.valueOf(item.getModelObject().getListPrice())));
		item.add(new Label("buyprice",String.valueOf(item.getModelObject().purchasePrice())));
		item.add(new Label("sellprice",String.valueOf(item.getModelObject().salePrice())));
		item.add(new Label("stock",String.valueOf(item.getModelObject().getStock())));
	}

	@Override
	protected void initializeService() {
		this.setService(this.getHome().getGeneralService().getStockableSer());
	}

	@Override
	protected IDataProvider setDataProvider() {
		return new StockableDataProvider(this.getItems());
	}

}
